<php>
    $ajax_url = cmf_plugin_url('ServerData://ServerData/index');
</php>
<style type="text/css">
    h1 {font-size: 26px; padding: 0; margin: 0; color: #333333; font-family:"Lucida Sans Unicode","Lucida Grande",sans-serif;}
    h1 small {font-size: 11px; font-family: Tahoma; font-weight: bold; }
    a{color: #666; text-decoration:none;}
    a.black{color: #000000; text-decoration:none;}
    .w_logo{height:25px;text-align:center;color:#333;font-size: 15px; width:13%; }
    .j_top{display:table;font-weight:bold;background:#dedede;color:#626262;width: 100%;text-align: left; height: 25px; line-height: 25px;
    box-shadow: 1px 1px 1px #CCC;
    -moz-box-shadow: 1px 1px 1px #CCC;
    -webkit-box-shadow: 1px 1px 1px #CCC;
    -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=135, Color='#CCCCCC')";}
    .j_top a{text-align:center; width:8.7%; display: table-cell; padding: 5px 0px;}
    .j_top a:hover{background:#dadada;}
    .con{width: 100%;}
    .con .j_top{ padding: 0px 10px;}
    .j_tb{display:table;width:100%;padding:5px 10px;border-bottom:1px solid #CCCCCC;text-align:left;}
    .j_tb:last-child{border:0;}
    .j_td{display:table-cell;}
    .j_td_t{width:140px;font-weight:bold;}
    .j_td_c{width:50%;}
    .j_td_t1{width:320px;}
    
    input{padding: 2px; background: #FFFFFF; border-top:1px solid #666666; border-left:1px solid #666666; border-right:1px solid #CCCCCC; border-bottom:1px solid #CCCCCC; font-size:12px}
    input.btn{font-weight: bold; height: 20px; line-height: 20px; padding: 0 6px; color:#666666; background: #f2f2f2; border:1px solid #999;font-size:12px}
    .bar {border:1px solid #999999; background:#FFFFFF; height:10px; font-size:2px; width:89%; margin:2px 0 5px 0;padding:1px; overflow: hidden;}
    .bar_1 {border:1px dotted #999999; background:#FFFFFF; height:10px; font-size:2px; width:89%; margin:2px 0 5px 0;padding:1px; overflow: hidden;}
    .barli_red{background:#ff6600; height:5px; margin:0px; padding:0;}
    .barli_blue{background:#0099FF; height:5px; margin:0px; padding:0;}
    .barli_green{background:#36b52a; height:5px; margin:0px; padding:0;}
    .barli_black{background:#333; height:5px; margin:0px; padding:0;}
    .barli_1{background:#999999; height:5px; margin:0px; padding:0;}
    .barli{background:#36b52a; height:5px; margin:0px; padding:0;}
    .server_data table th,
    .server_data table td{padding:5px;}

    .wrap table tr th{
        background: none;
        color:#2C3E50;
        font-weight: bold;
    }
    body{color:#2C3E50;font-size: 13px;}
    #cpuuse table tr th{
        border:1px #ecf0f1 solid;
    }
</style>
<script type="text/javascript">
    var on = 1;
    $(document).ready(function(){
        getJSONData();
        // $.getJSON('{$ajax_url}', displayData);
    });

    var is_cpu_use = {$is_cpu_use};
    function getJSONData(){
        if( is_cpu_use==1 ){
            setTimeout("getJSONData()", 5000);
        }
        $.getJSON('{$ajax_url}', displayData);
    }
    function displayData(dataJSON){
        $("#useSpace").html(dataJSON.useSpace);
        $("#freeSpace").html(dataJSON.freeSpace);
        $("#hdPercent").html(dataJSON.hdPercent);
        $("#barhdPercent").width(dataJSON.barhdPercent+'%');
        $("#TotalMemory").html(dataJSON.TotalMemory);
        $("#UsedMemory").html(dataJSON.UsedMemory);
        $("#FreeMemory").html(dataJSON.FreeMemory);
        $("#CachedMemory").html(dataJSON.CachedMemory);
        $("#Buffers").html(dataJSON.Buffers);
        $("#TotalSwap").html(dataJSON.TotalSwap);
        $("#swapUsed").html(dataJSON.swapUsed);
        $("#swapFree").html(dataJSON.swapFree);
        $("#swapPercent").html(dataJSON.swapPercent);
        $("#loadAvg").html(dataJSON.loadAvg);
        $("#uptime").html(dataJSON.uptime);
        $("#cpuuse").html(dataJSON.cpuuse);
        $("#memRealUsed").html(dataJSON.memRealUsed);
        $("#memRealFree").html(dataJSON.memRealFree);
        $("#memRealPercent").html(dataJSON.memRealPercent);
        $("#memPercent").html(dataJSON.memPercent);
        $("#barmemPercent").width(dataJSON.memPercent+'%');
        $("#barmemRealPercent").width(dataJSON.barmemRealPercent+'%');
        $("#memCachedPercent").html(dataJSON.memCachedPercent);
        $("#barmemCachedPercent").width(dataJSON.barmemCachedPercent+'%');
        $("#barswapPercent").width(dataJSON.barswapPercent+'%');
        $('#cpuuse .cpu-main').html(dataJSON.cpu_use);

        if( on == 1 ){
            dragulaInit();
        }
        on = 0;
    }

    function dragulaInit(){
        Wind.css('dragula');
        Wind.use('masonry', 'imagesloaded', 'dragula', function () {
            var $homeGrid = $('.home-grid').masonry({
                // set itemSelector so .grid-sizer is not used in layout
                itemSelector: '.grid-item',
                // use element for option
                columnWidth: '.grid-sizer',
                percentPosition: true,
                horizontalOrder: false,
                transitionDuration: 0
            });

            $homeGrid.masonry('on', 'layoutComplete', function (event, laidOutItems) {
            });


            $homeGrid.masonry();

            var containers = [];
            $('.home-grid .grid-item').each(function () {
                containers.push(this);
            });
            dragula(containers, {
                isContainer: function (el) {
                    return false; // only elements in drake.containers will be taken into account
                },
                moves: function (el, source, handle, sibling) {
                    return true; // elements are always draggable by default
                },
                accepts: function (el, target, source, sibling) {
                    return true; // elements can be dropped in any of the `containers` by default
                },
                invalid: function (el, handle) {
                    return false; // don't prevent any drags from initiating by default
                },
                direction: 'vertical',             // Y axis is considered when determining where an element would be dropped
                copy: false,                       // elements are moved by default, not copied
                copySortSource: false,             // elements in copy-source containers can be reordered
                revertOnSpill: false,              // spilling will put the element back where it was dragged from, if this is true
                removeOnSpill: false,              // spilling will `.remove` the element, if this is true
                mirrorContainer: document.body,    // set the element that gets mirror elements appended
                ignoreInputTextSelection: true     // allows users to select input text, see details below
            }).on('drop', function (el, target, source, sibling) {
                var $target          = $(target);
                var targetClasses    = $target.attr('class');
                var targetDataWidget = $target.data('widget');
                var targetDataSystem = $target.data('system');
                var $source          = $(source);
                var sourceClasses    = $source.attr('class');
                var sourceDataWidget = $source.data('widget');
                var sourceDataSystem = $source.data('system');
                $(source).append($(target).find('.dashboard-box').not('.gu-transit'));
                $(target).append(el);
                $target.attr('class', sourceClasses);
                $target.data('widget', sourceDataWidget);
                $target.data('system', sourceDataSystem);

                $source.attr('class', targetClasses);
                $source.data('widget', targetDataWidget);
                $source.data('system', targetDataSystem);
                $homeGrid.masonry();

                // _widgetSort();
            }).on('shadow', function (el, container, source) {
                $homeGrid.masonry();
            });
        });
    }

    
</script>
<div class="panel panel-default server_data">
    <div class="panel-heading">
        <h3 class="panel-title">{:lang('SERVER_DATA')}</h3>
    </div>
    <div class="panel-body home-info">
        <!-- <ul class="list-unstyled">
            <volist name="server_info" id="vo">
                <li><em>{$key}</em> <span>{$vo}</span></li>
            </volist>
        </ul> -->
        <!--Server Real-Time-->
        <div class="con">
            <!-- <div class="j_top">服务器实时数据</div> -->
            <div class="j_tb">
                <div class="j_td" style="width: 50%">
                    <span class="j_td j_td_t">服务器已运行时间</span>
                    <span class="j_td" id="uptime">{$server_info['uptime']|default=''}</span>
                </div>
            </div>

            <div class="j_tb">
                <span class="j_td j_td_t">系统平均负载</span>
                <span class="j_td" id="loadAvg">{$server_info['loadAvg']|default=''}</span>
            </div>

            <div class="j_tb">
                <span class="j_td j_td_t">硬盘使用状况</span>
                <span>总空间 {$hd['t']|default=''}&nbsp;G，已用 <font color='#333333'><span id="useSpace">{$hd['u']|default=''}</span></font>&nbsp;G，
                    空闲 <font color='#333333'><span id="freeSpace">{$hd['f']|default=''}</span></font>&nbsp;G，
                    使用率 <span id="hdPercent">{$hd['PCT']|default=''}</span>%
                    <div class="bar"><div id="barhdPercent" class="barli_black" style="width:{$hd['PCT']|default=''}%">&nbsp;</div> </div>
                </span>
            </div>

            <div class="j_tb"><span class="j_td j_td_t">内存使用状况</span>
                <span class="j_td">物理内存：共<font color='#CC0000'>{$server_info['memTotal']|default=''} </font>
                    , 已用<font color='#CC0000'><span id="UsedMemory">{$server_info['memUsed']|default=''}</span></font>
                    , 空闲<font color='#CC0000'><span id="FreeMemory">{$server_info['memFree']|default=''}</span></font>
                    , 使用率<span id="memPercent">{$server_info['memPercent']|default=''}</span>
                    <div class="bar">
                        <div id="barmemPercent" class="barli_red" style="width:{$server_info['memPercent']|default=''}%">&nbsp;</div>
                    </div>

                    <if condition="$server_info['memCached']">
                        Cache化内存为 <span id="CachedMemory">{$server_info['memCached']|default=''}</span>
                        , 使用率<span id="memCachedPercent">{$server_info['memCachedPercent']|default=''}</span>% |
                        Buffers缓冲为  <span id="Buffers">{$server_info['memBuffers']|default=''}</span>
                        <div class="bar"><div id="barmemCachedPercent" class="barli_blue" style="width:{$server_info['memCachedPercent']|default=''}%">&nbsp;</div></div>
                        真实内存使用<span id="memRealUsed">{$server_info['memRealUsed']|default=''}</span>
                        , 真实内存空闲<span id="memRealFree">{$server_info['memRealFree']|default=''}</span>
                        , 使用率<span id="memRealPercent">{$server_info['memRealPercent']|default=''}</span>%
                        <div class="bar_1"><div id="barmemRealPercent" class="barli_1" style="width:{$server_info['memRealPercent']|default=''}%">&nbsp;</div></div>
                    </if>

                    <if condition="$server_info['swapTotal']">
                        SWAP区：共{$server_info['swapTotal']|default=''}
                        , 已使用<span id="swapUsed">{$server_info['swapUsed']|default=''}</span>
                        , 空闲<span id="swapFree">{$server_info['swapFree']|default=''}</span>
                        , 使用率<span id="swapPercent">{$server_info['swapPercent']|default=''}</span>%
                        <div class="bar"><div id="barswapPercent" class="barli_red" style="width:{$server_info['swapPercent']|default=''}%">&nbsp;</div> </div>
                    </if>
                </span>
            </div>

            <div class="j_tb">
                <span class="j_td j_td_t">CPU型号[{$server_info['cpu']['num']|default=''}核]</span>
                <span>{$server_info['cpu']['model']|default=''}</span>
            </div>

            <div class="j_tb"><span class="j_td j_td_t">CPU使用情况</span>
                <div id="cpuuse">
                    <table class="table table-hover table-bordered table-list">
                        <tr>
                            <th>cpu</th>
                            <th>us(用户空间占用)</th>
                            <th>sy(内核空间占用)</th>
                            <th>ni(改变过优先级的进程占用)</th>
                            <th>id(空闲)</th>
                            <th>wa(IO等待占用)</th>
                            <th>irq(硬中断)</th>
                            <th>softirq(软中断)</th>
                        </tr>
                        <tbody class="cpu-main">
                            {$server_info['cpu_use']|default=''}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
